
import Pendataan.Pendataan;
import Pendataan.PendataanKontrol;
import Pinjam.Data;
import Pinjam.PinjamKontrol;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import kembali.KembaliKontrol;
import kembali.kembali;
import kembali.kembaliTableModel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author user
 */
public class KembaliJInternalFrame1 extends javax.swing.JInternalFrame {

    /**
     * Creates new form KembaliJInternalFrame
     */
    public KembaliJInternalFrame1() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
     public class JTextFieldFilter extends PlainDocument {

        public static final String numeric = "0123456789";
        public static final String huruf = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXXYZ";
        protected String acceptedChars = null;
        protected boolean negativeAccepted = false;

        public JTextFieldFilter(String acceptedchars) {
            acceptedChars = acceptedchars;
        }

        public void setNegativeAccepted(boolean negativeaccepted) {
            if (acceptedChars.equals(numeric) || (acceptedChars.equals(huruf))) {
                negativeAccepted = negativeaccepted;
                acceptedChars += "_";
            }

        }

        public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
            if (str == null) {
                return;
            }

            for (int i = 0; i < str.length(); i++) {
                if (acceptedChars.indexOf(str.valueOf(str.charAt(i))) == -1) {
                    return;
                }
                if (negativeAccepted && str.indexOf("_") != -1) {
                    if (str.indexOf("_") != 0 || offset != 0) {
                        return;
                    }
                }
                super.insertString(offset, str, attr);
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        NIMBukuTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        okButtonKembali = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        kembaliTable = new javax.swing.JTable();
        judulBukuTextField1 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("Kembali"), this, org.jdesktop.beansbinding.BeanProperty.create("title"));
        bindingGroup.addBinding(binding);

        NIMBukuTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NIMBukuTextFieldActionPerformed(evt);
            }
        });

        jLabel1.setText("Judul Buku");

        okButtonKembali.setText("OK");
        okButtonKembali.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonKembaliActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 0, 204));
        jLabel2.setText("PERPUSTAKAAN");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(0, 0, 204));
        jLabel3.setText("UNIVERSITAS SANATA DHARMA");

        kembaliTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "noBuku", "judul buku", "nama penerbit", "kuota"
            }
        ));
        kembaliTable.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                kembaliTableAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        jScrollPane1.setViewportView(kembaliTable);

        judulBukuTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                judulBukuTextField1ActionPerformed(evt);
            }
        });

        jLabel4.setText("NIM");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(judulBukuTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(NIMBukuTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 385, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 38, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(okButtonKembali, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(79, 79, 79))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(142, 142, 142))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addGap(55, 55, 55)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(NIMBukuTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(judulBukuTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(okButtonKembali)
                .addGap(219, 219, 219))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonKembaliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonKembaliActionPerformed
        String judulBuku = judulBukuTextField1.getText();
        String nim = NIMBukuTextField.getText();

        kembali data = new kembali();
        data.setJudulBuku(judulBuku);
        data.setNIM(nim);


        Pendataan kat = new Pendataan();
        String judul = judulBukuTextField1.getText();
        kat.setJudulBuku(judul);


        try {
            KembaliKontrol kembali;
            kembali = KembaliKontrol.getKoneksiKembali();
            kembali.KembaliData(data);


            PendataanKontrol Pend;
            Pend = PendataanKontrol.getKoneksiPendataan();
            Pend.kuota(kat);


//            List<Pendataan> pdt = PendataanKontrol.getKoneksiBarang().kuota(judulBuku);
            kembali.hapusData(data);
            if (judulBukuTextField1.getText().equals("") || NIMBukuTextField.getText().equals("")) {
                try {
                    throw new Exception("Ada field yang belum diisi");
                } catch (Exception z) {
                    JOptionPane.showMessageDialog(rootPane, "ERROR : " + z.getMessage());
                }
            } else {
                JOptionPane.showMessageDialog(this, "data kembali berhasil disimpan \n"
                        + "NIM = " + nim + "\n"
                        + "judul buku = " + judulBuku + "\n");
//                        + "tanggalPinjam = " + tglPinjam + "\n"
//                        + "tanggalKembali =" + tglKembali + "\n"

//                judulBukuTextField1.setText(judulBuku);
//                NIMBukuTextField.setText(nim);
////                tanggalPinjamTextFieldPinjam.setText(tglPinjam);
////                tanggalKembaliTextFieldPinjam2.setText(tglKembali);
//
            }
        } catch (SQLException ex) {

            JOptionPane.showMessageDialog(this, "Data Gagal Disimpan");
            Logger.getLogger(PinjamJInternalFrame1.class.getName()).log(Level.SEVERE, null, ex);
        }
//    

    }//GEN-LAST:event_okButtonKembaliActionPerformed

    private void kembaliTableAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_kembaliTableAncestorAdded
        // TODO add your handling code here
    }//GEN-LAST:event_kembaliTableAncestorAdded

    private void NIMBukuTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NIMBukuTextFieldActionPerformed
        // TODO add your handling code here:
//         String judul = judulBukuTextField1.getText();
        String nim = NIMBukuTextField.getText();
//        String kdkategori = kategoriTextField.getText();
        NIMBukuTextField.setText(nim);
//        judulBukuTextField1.setText(judul);
//      kategoriTextField.setText(kdkategori);
        try {
//            Barang cariBarang = BarangKontrol.getKoneksiBarang().cariberdasrkanKode(kodeBarang);
            Data kembaliBuku = PinjamKontrol.getKoneksiPinjam().cariberdasrkanKode(nim);
//            Data kembaliBuku = PinjamKontrol.getKoneksiBarang().cariberdasrkanKode(nim);
            if (kembaliBuku != null) {
//              
                judulBukuTextField1.setText(kembaliBuku.getJudulBuku());
//                String satuan = cariBarang.getSatuan();
//                SatuanTextField.setText(cariBarang.getSatuan());


                //untuk kategori;

//                String namaKategori = cariBarang.getKategori().getNamaKategori();
//                kategoriTextField.setText(namaKategori);
                isiStock();
//                List<kembali> kembali = kembaliKontrol1.getKoneksiKategori().lihatseluruhKategori();


            } else {
//                       System.out.println("tidak ditemukan");
                JOptionPane.showMessageDialog(null, "data tidak ditemukan");

            }
        } catch (SQLException ex) {
            Logger.getLogger(KembaliJInternalFrame1.class.getName()).log(Level.SEVERE, null, ex);
        }
//        
//        String judulBuku = judulBukuTextField.getText();
//        judulBukuTextField.setText(judulBuku);
//
//        try {
//            kembaliKontrol1 kembali;
//             List<kembali> judul = kembaliKontrol1.getKoneksiKategori().lihatseluruhKategori();
////            judulBukuTextField.setText(judul);
//        } catch (SQLException ex) {
//            Logger.getLogger(KembaliJInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }//GEN-LAST:event_NIMBukuTextFieldActionPerformed

    private void judulBukuTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_judulBukuTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_judulBukuTextField1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField NIMBukuTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField judulBukuTextField1;
    private javax.swing.JTable kembaliTable;
    private javax.swing.JButton okButtonKembali;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    public void isiStock() throws SQLException {
//        String judulBuku = judulBukuTextField1.getText();
        String nim = NIMBukuTextField.getText();
        List<kembali> kembali = KembaliKontrol.getKoneksiKembali().lihatseluruhKategori();
        kembaliTableModel model1 = new kembaliTableModel(kembali);
        kembaliTable.setModel(model1);
    }
//     public void isikuota() throws SQLException {
////        String judulBuku = judulBukuTextField1.getText();
//        String judul = judulBukuTextField1.getText();
//        Pendataan pendataans;
//        pendataans = PendataanKontrol.getKoneksiBarang().kuota(judul);
//        kembaliTableModel model1 = new kembaliTableModel(kembali);
//        kembaliTable.setModel(model1);
//    }
}
